﻿using Ewelve_Mouth_HD.Model;

namespace Ewelve_Mouth_HD.DAL
{
    public class RoomDal
    {
        private readonly Mycontext context;

        public RoomDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 排房添加
        /// </summary>
        /// <param name="W"></param>
        /// <returns></returns>
        public int RoomAdd(Room W)
        {
            try
            {
                context.Add(W);
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageSum"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IQueryable RoomShowAll(int PageIndex,int PageSize,out int PageSum,out int PageCount)
        {
            try
            {
                var Y = context.rooms.Join(context.RoomNubs, A => A.RoomEng, B => B.NubId, (A, B) => new
                {
                    A.RId,
                    A.RoomEng,
                    A.TypeEng,
                    B.NubId,
                    B.NubCard,
                    B.IsDelete
                }).Where(R=>R.IsDelete==false);
                var U = Y.Join(context.RoomTypes, A => A.TypeEng, B => B.RoomId, (A, B) => new 
                {
                    A.RId,
                    A.RoomEng,
                    A.TypeEng,
                    A.NubId,
                    A.NubCard,
                    B.RoomId,
                    B.RoomName,
                    B.IsDelete
                }).Where(R => R.IsDelete == false);
                PageSum = U.Count();
                PageCount = (int)Math.Ceiling((decimal)PageSum / PageSize);
                return Y.OrderBy(O => O.RId).Skip((PageIndex - 1) * PageSize).Take(PageSize);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
